﻿using System.Collections.Generic;
using Game;
using Mekiasm.Common;
using TemplatesDatabase;
using static Mekiasm.ComponentEnergyMachine;
namespace Mekiasm
{
    public class SaveItemSystem
    {
        public static List<SaveItem> toolSaveItems = new List<SaveItem>();
        public static int GetAvailableID(int Index)
        {
            for (int i = 1; i < 65535; i++)
            {
                SaveItem toolSaveItem = toolSaveItems.Find(p => p.id == i && p.Index == Index);
                if (toolSaveItem == null) return i;
            }
            return 0;
        }
        public static int StoreBlockData(int value, long power,int fuelValue = 0, int slotCount = 0)
        {
            int Index = ILibrary.GetBasicValue(value);
            int freeID = GetAvailableID(Index);
            SaveItem toolSaveItem = new SaveItem() { id = freeID, fuelAmount = power, Index = Index,fuelValue=fuelValue };
            toolSaveItems.Add(toolSaveItem);
            int newV = ILibrary.setExtraData(value, freeID);
            return newV;
        }
        public static bool GetBlockData(int value, out SaveItem toolSaveItem)
        {
            int Index = ILibrary.GetBasicValue(value);
            int id = ILibrary.getExtraData(value);
            toolSaveItem = toolSaveItems.Find(p => p.id == id && p.Index == Index);
            return toolSaveItem != null;
        }
        public static int RemoveBlockData(int value, SaveItem saveItem)
        {
            if (toolSaveItems.Contains(saveItem)) toolSaveItems.Remove(saveItem);
            return ILibrary.setExtraData(value, 0);
        }
        public static ValuesDictionary Save()
        {
            ValuesDictionary valuesDictionary = new ValuesDictionary();
            int i = 0;
            foreach (SaveItem toolSaveItem in toolSaveItems)
            {
                ValuesDictionary mainItem = new ValuesDictionary();
                mainItem.SetValue("id", toolSaveItem.id);
                mainItem.SetValue("index", toolSaveItem.Index);
                mainItem.SetValue("fuelAmount", toolSaveItem.fuelAmount);
                mainItem.SetValue("fuelValue", toolSaveItem.fuelValue);
                toolSaveItem.Save(mainItem);
                valuesDictionary.SetValue(i++.ToString(), mainItem);
            }
            return valuesDictionary;
        }
        public static void Load(ValuesDictionary valuesDictionary)
        {
            foreach (ValuesDictionary valuePairs in valuesDictionary.Values)
            {
                SaveItem toolSaveItem = new SaveItem()
                {
                    fuelAmount = valuePairs.GetValue<long>("fuelAmount"),
                    fuelValue = valuePairs.GetValue<int>("fuelValue"),
                    id = valuePairs.GetValue<int>("id"),
                    Index = valuePairs.GetValue<int>("index")
                };
                toolSaveItem.Load(valuePairs);
                toolSaveItems.Add(toolSaveItem);
            }
        }
        /// <summary>
        /// 没有创建，归零清除
        /// </summary>
        /// <param name="value"></param>
        /// <param name="power"></param>
        /// <param name="fuelValue"></param>
        public static void ChangePower(IInventory inventory, int slotIndex, long power, int fuelValue = 0)
        {
            int value = inventory.GetSlotValue(slotIndex);
            int count = inventory.GetSlotCount(value);
            GetBlockData(value,out var item);
            if (item == null)
            {
                value = StoreBlockData(value, power, fuelValue);
                inventory.RemoveSlotItems(slotIndex, count);
                inventory.AddSlotItems(slotIndex, value, count);
            }
            else
            {
                item.fuelAmount += power;
                item.fuelValue = fuelValue;
                if (item.fuelAmount == 0 && BlocksManager.Blocks[Terrain.ExtractContents(value)] is MekBlock mek)
                {
                    if (mek.GetItem(ref value).IsCrushWhenNoPower())
                    {
                        inventory.RemoveSlotItems(slotIndex, count);
                    }
                    else
                    {
                        RemoveBlockData(value, item);
                        value = ILibrary.setExtraData(value, 0);
                        inventory.RemoveSlotItems(slotIndex, count);
                        inventory.AddSlotItems(slotIndex, value, count);
                    }
                }
            }
        }

        public static int ChangePower(int value, long power, int fuelValue = 0)
        {
            GetBlockData(value, out var item);
            if (item == null)
            {
                return StoreBlockData(value, power, fuelValue);
            }
            else
            {
                item.fuelAmount += power;
                item.fuelValue = fuelValue;
            }
            if (item.fuelAmount == 0)
            {
                if (BlocksManager.Blocks[Terrain.ExtractContents(value)] is MekBlock mek)
                {
                    if (mek.GetItem(ref value).IsCrushWhenNoPower())
                    {
                        return 0;
                    }
                    else
                    {
                        RemoveBlockData(value, item);
                        return ILibrary.GetBasicValue(value);
                    }
                }
            }
            return value;
        }

        public static void Dispose()
        {
            toolSaveItems.Clear();
        }
    }
}
